<?php
/**
 * Created by PhpStorm.
 * User: tom_xia
 * Date: 2018/6/4
 * Time: 13:21
 * pv统计入库
 */
require_once dirname(dirname(__DIR__)) . "/common.php";
use app\lib\db\DbException;
use app\cls\tom\Queue;
use app\cls\tom\SysResource;
use app\cls\tom\Process;

$process = new Process(__FILE__);
$process->setProcessNum(5);
$process->initProcess();

$redis = SysResource::getRedis();
$db =  SysResource::getDB(true);

try{
    while(true){
        for($i = 1 ;$i<=5;$i++){
            for($j = 0 ;$j < 100;$j++){
                //test为测试，no_test为正式
                $PATH="no_test";
                if($PATH=="no_test"){
                    $key = REDIS_PREFIX."H5_statics_".$i;
                }else if($PATH=="test"){
                    $key = REDIS_PREFIX."test_H5_statics_".$i;
                }
                $dataStr = $redis->rPop($key);
                $result =  json_decode($dataStr,true);
                $d['name']=$result['name'];
                $d['add_time']=$result['add_time'];
//                $d['add_time']=date("Y-m-d H:i:s",time());

                if($result){
                    $db->insert("h5_statics",$d);
                    $re = $db->insert_id();
                    //如果分享数为0 或者分享数为空
                    if(!$re){
                        if($i==1){
                            $redis->lPush($key2,$dataStr);
                        }else if($i==2){
                            $redis->lPush($key3,$dataStr);
                        }else if($i==3){
                            $redis->lPush($key4,$dataStr);
                        }else if($i==4){
                            $redis->lPush($key5,$dataStr);
                        }else if($i==5){
                            $redis->lPush($key1,$dataStr);
                        }
                    }
                }else{
                    continue;
                }
            }
        }
        sleep(1);
        $process->checkProcessTime(true);
    }
}catch (Exception $e){
    $process->log($e->getMessage());
}
